<?php


namespace app\api\controller;


use app\BaseController;
use app\service\apixcxService;
use app\service\deviceService;
use app\service\wotuService;
use think\facade\Db;
use think\facade\Log;

class Device extends BaseController
{
    public function info(){
        $id=request()->get("id");

        $info=Db::table("device")->where("qrcode_id",$id)->find();
        if(empty($info)){
            parent::sendOutput("设备信息不存在",500);
        }
        $info["community_name"]="";
        parent::sendOutput($info);
    }
    public function open(){
        Log::error('这是一条错误日志');
        $code=request()->get("auth_code");
        $id=request()->get("id");
        $api = new apixcxService();
        //获取当前用户支付宝ID
        $oauthtoken = $api->xcxoauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            parent::sendOutput($oauthtoken->sub_msg,500);
        }
        $user_id = $oauthtoken->open_id;
        $info=Db::table("device")->where("qrcode_id",$id)->find();
        if(empty($info)){
            parent::sendOutput("设备信息不存在",500);
        }
        //parent::sendOutput("审核中",500);
        $user=Db::table("room_user")->where("user_id",$user_id)->find();
        if(!empty($user)){
            $add_log["room_id"]=$user["room_id"];
            $add_log["user_type"]=$user["type"];
            $add_log["nick_name"]=$user["nick_name"];
            $add_log["name"]=$user["name"];
            $add_log["mobile"]=$user["mobile"];
            $add_log["avatar"]=$user["avatar"];
            $user_type=2;//业主家人
            //parent::sendOutput($res->sub_msg,500);
        }else{
            //查询是不是外卖快递
            $stranger=Db::table("stranger_user")
                ->where("user_id",$user_id)
                ->find();
            if(!empty($stranger)){
                $add_log["stranger_id"]=$stranger["id"];
                $add_log["name"]=$stranger["name"];
                $add_log["mobile"]=$stranger["mobile"];
                $add_log["user_type"]=$stranger["user_type"];
                $user_type=3;//业主家人
                //parent::sendOutput($res->sub_msg,500);
            }else{
                $data["company_code"]="MYFW";//"SAASCHANNEL";//G100115971008750 SAASCHANNEL
                $data["company_name"]="龙湖物业";
                $data["city_code"]="430100";
                $data["delivery_user_id"]=$user_id;
                $res=$api->riderDetect(json_encode($data));
                $user_type="";
                if($res->code!="10000"){
                    //parent::sendOutput($res->sub_msg,500);
                    //不是骑手，查询是否是业主家人
                    //查询是不是审核中的陌生人
                    $applyInfo=Db::table("stranger_apply")
                        ->where("user_id",$user_id)
                        ->where("qrcode_id",$id)
                        ->where("status",1)
                        ->find();
                    if(!empty($applyInfo)){
                        parent::sendOutput("审核中",10001);
                    }
                    $test["user_id"]=$user_id ;
                    $test["create_time"]=date("y-m-d H;i:s");
                    $test["sub_msg"]=$res->sub_msg;
                    Db::table("test")->insert($test);
                    Log::error($res->sub_msg."--".$user_id );
                    parent::sendOutput($res->sub_msg,500);
                }else{
                    $user_type=1;
                }

            }

        }




        $add_log["user_id"]=$user_id;
        $add_log["device_id"]=$info["id"];
        $add_log["device_name"]=$info["lockname"];
        $add_log["community_name"]=$info["community_name"];
        $add_log["community_id"]=$info["community_id"];
        $add_log["device_type"]=$info["deviceType"];
        $add_log["create_time"]=date("Y-m-d H:i:s");
        if($info["lock_type"]==1){
            $api=new wotuService();
            $res=$api->devopen($info["devicekey"]);
            if($res["code"]=="WO_SUS1000"){
                $result["msg"]="开门成功";
                $result["userid"]=$user_id;

                $add_log["open_status"]="success";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($result);
            }else{
                $add_log["open_status"]="fail";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($res["msg"],500);
            }
        }else{

            $msg=(new deviceService())->open($id);

            if($msg=="success"){
                $result["msg"]="开门成功";
                $result["userid"]=$user_id;

                $add_log["open_status"]="success";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($result);
            }else{
                $add_log["open_status"]="fail";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($msg,500);
            }
        }


    }
    public function open_old(){
        $code=request()->get("auth_code");
        $id=request()->get("id");
        $api = new apixcxService();
        //获取当前用户支付宝ID
        $oauthtoken = $api->xcxoauthToken($code);
        if(property_exists($oauthtoken,'sub_msg')){
            parent::sendOutput($oauthtoken->sub_msg,500);
        }
        $user_id = $oauthtoken->open_id;
        $info=Db::table("device")->where("qrcode_id",$id)->find();
        if(empty($info)){
            parent::sendOutput("设备信息不存在",500);
        }

        $data["company_code"]="MYFW";//"SAASCHANNEL";//G100115971008750 SAASCHANNEL
        $data["company_name"]="龙湖物业";
        $data["city_code"]="430100";
        $data["delivery_user_id"]=$user_id;
        $res=$api->riderDetect(json_encode($data));
        $user_type="";
        if($res->code!="10000"){
            //parent::sendOutput($res->sub_msg,500);
            //不是骑手，查询是否是业主家人
            $user=Db::table("room_user")->where("user_id",$user_id)->find();
            if(!empty($user)){
                $add_log["room_id"]=$user["room_id"];
                $add_log["user_type"]=$user["type"];
                $add_log["nick_name"]=$user["nick_name"];
                $add_log["mobile"]=$user["mobile"];
                $add_log["avatar"]=$user["avatar"];
                $user_type=2;//业主家人
                //parent::sendOutput($res->sub_msg,500);
            }else{
                //查询是不是外卖快递
                $stranger=Db::table("stranger_user")
                    ->where("user_id",$user_id)
                    ->find();
                if(!empty($stranger)){
                    $add_log["stranger_id"]=$stranger["id"];
                    $add_log["name"]=$stranger["name"];
                    $add_log["mobile"]=$stranger["mobile"];
                    $add_log["user_type"]=$stranger["user_type"];
                    $user_type=3;//业主家人
                    //parent::sendOutput($res->sub_msg,500);
                }else{
                    //查询是不是审核中的陌生人
                    $applyInfo=Db::table("stranger_apply")
                        ->where("user_id",$user_id)
                        ->where("qrcode_id",$id)
                        ->where("status",1)
                        ->find();
                    if(!empty($applyInfo)){
                        parent::sendOutput("审核中",10001);
                    }
                    parent::sendOutput($res->sub_msg,500);
                }

            }
        }else{
            $user_type=1;
        }


        $result["showOwnerBtn"]=true;

        $add_log["user_id"]=$user_id;
        $add_log["device_id"]=$info["id"];
        $add_log["device_name"]=$info["lockname"];
        $add_log["community_name"]=$info["community_name"];
        $add_log["community_id"]=$info["community_id"];
        $add_log["device_type"]=$info["deviceType"];
        $add_log["create_time"]=date("Y-m-d H:i:s");
        if($info["lock_type"]==1){
            $api=new wotuService();
            $res=$api->devopen($info["devicekey"]);
            if($res["code"]=="WO_SUS1000"){
                $result["msg"]="开门成功";
                $result["userid"]=$user_id;

                $add_log["open_status"]="success";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($result);
            }else{
                $add_log["open_status"]="fail";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($res["msg"],500);
            }
        }else{

            $msg=(new deviceService())->open($id);

            if($msg=="success"){
                $result["msg"]="开门成功";
                $result["userid"]=$user_id;

                $add_log["open_status"]="success";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($result);
            }else{
                $add_log["open_status"]="fail";
                if($user_type=="1"){
                    Db::table("records_rider")->insert($add_log);
                }else if ($user_type=="2"){
                    Db::table("records")->insert($add_log);
                }else if ($user_type=="3"){
                    Db::table("records_stranger")->insert($add_log);
                }
                parent::sendOutput($msg,500);
            }
        }


    }
}